package mshop.com.schild_kotlin.adapters

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import mshop.com.schild_kotlin.R
import mshop.com.schild_kotlin.activitys.CateGoryActivity
import mshop.com.schild_kotlin.entitys.FindBean
import mshop.com.schild_kotlin.utils.ActivitysUtil

/**
 * Created by mayi on 2018/5/27.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
class FindAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var datas: MutableList<FindBean> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {
        val view = parent?.let { LayoutInflater.from(it.context).inflate(R.layout.item_find_adapter, parent, false) } as View
        return ItemViewHolder(view)
    }

    override fun getItemCount(): Int {
        return datas.size ?: 0
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        (holder as ItemViewHolder).onBind(datas.get(position))
    }


    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var iv_photo: ImageView = itemView.findViewById(R.id.iv_photo)
        var tv_title: TextView = itemView.findViewById(R.id.tv_title)
        var iv_photo_bg: ImageView = itemView.findViewById(R.id.iv_photo_bg)

        init {
            iv_photo_bg.setOnClickListener {
                val intent = Intent(it.context, CateGoryActivity::class.java)
                intent.putExtra("categoryName", tv_title.text.toString())
                it.context.startActivity(intent)
            }
        }

        fun onBind(findBean: FindBean) {
            tv_title.text = findBean.name
            Glide.with(itemView.context).load(findBean.bgPicture).into(iv_photo)
        }

    }

}